﻿using System.Net.Http.Json;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace BaseData.RazorLib.Services;

public class WorkstationService : IWorkstationService
{
    private readonly IHttpClientFactory _httpClientFactory;

    public WorkstationService(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public async Task<PaginatedResult<WorkstationInfoResponseDto>?> QueryWorkstationsByPage(
        WorkstationSearchRequestDto searchRequestDto)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("basedataApi");
            var response = await httpClient.GetAsync(
                searchRequestDto.ToQueryString(ApiRoutes.Workstation.QueryWorkstations));
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<PaginatedResult<WorkstationInfoResponseDto>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<WorkstationInfoResponseDto>?> UpdateWorkstation(int id,
        WorkstationEditRequestDto editRequestDto)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("basedataApi");
            var response = await httpClient.PutAsJsonAsync(
                $"/{ApiRoutes.Workstation.UpdateWorkstation.Replace("{id}", id.ToString())}", editRequestDto);
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<WorkstationInfoResponseDto>>(resultJson);
                }
            }

            return new Result<WorkstationInfoResponseDto>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<WorkstationInfoResponseDto>?> CreateWorkstation(WorkstationEditRequestDto model)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("basedataApi");
            var response =
                await httpClient.PostAsJsonAsync($"/{ApiRoutes.Workstation.CreateWorkstation}", model);
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<WorkstationInfoResponseDto>>(resultJson);
                }
            }

            return new Result<WorkstationInfoResponseDto>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<WorkstationInfoResponseDto>?> GetWorkstation(int key)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("basedataApi");
            var response =
                await httpClient.GetAsync($"/{ApiRoutes.Workstation.GetWorkstation.Replace("{id}", key.ToString())}");
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<WorkstationInfoResponseDto>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<IList<WorkstationInfoResponseDto>>?> QueryWorkstations(
        WorkstationSearchRequestDto searchRequestDto)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("basedataApi");
            var response = await httpClient.GetAsync(
                searchRequestDto.ToQueryString(ApiRoutes.Workstation.QueryWorkstations));
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<IList<WorkstationInfoResponseDto>>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}